/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spreadsheet.containers;

import com.eteks.jeks.JeksCell;
import com.eteks.jeks.JeksCellEditor;
import com.eteks.jeks.JeksExpressionSyntax;
import com.eteks.jeks.JeksTable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import spreadsheet.spreadsheet.Table.Spreadsheet;
import spreadsheet.storablespreadsheet.StorableDataFrame;

/**
 *
 * @author Nobody
 */
public final class SpreadsheetWithFormula implements ISpreadsheetContainer {

    public StorableDataFrame exportAsStorableDataFrame() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private Spreadsheet excelTable;
    private JTextField textField;

    public Spreadsheet getSpreadsheet() {
        return excelTable;
    }

    public JTextField getTextField() {
        return textField;
    }

    public SpreadsheetWithFormula(Spreadsheet excelTable, JTextField textField) {
        this.excelTable = excelTable;
        this.textField = textField;
        initExcelTable();
    }

    public void initExcelTable() {
        excelTable.getModel().addTableModelListener(changeListener);
        excelTable.getSelectionModel().addListSelectionListener(selectionListener);
        excelTable.getColumnModel().addColumnModelListener(tableColumnModelListener);

    }

    public void initFormula() {

        textField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                JTable table = getSpreadsheet();
                String text = textField.getText();
                if (table.getCellEditor(0, 0) instanceof JeksCellEditor) {
                    int editedRow = table.getSelectedRow();
                    Object value = ((JeksCellEditor) table.getCellEditor(0, 0)).getModelValue(text, table.getModel(),
                            new JeksCell(editedRow,
                            table.convertColumnIndexToModel(table.getSelectedColumn())));
                    if (value != null) {
                        table.setValueAt(value, table.getSelectedRow(), table.getSelectedColumn());
                        table.setRowSelectionInterval(editedRow + 1, editedRow + 1);
                    }
                }
            }
        });
    }

    public void pasteCopiedData() {
        JTable table = getSpreadsheet();
        if (table instanceof JeksTable
                && ((JeksTable) table).pasteCopiedCells()) {
            JeksExpressionSyntax syntax = (JeksExpressionSyntax) ((JeksTable) table).getExpressionParser().getSyntax();
            JOptionPane.showMessageDialog(null,
                    syntax.getMessage(JeksExpressionSyntax.MESSAGE_CIRCULARITY_ERROR_INFO),
                    syntax.getMessage(JeksExpressionSyntax.MESSAGE_CIRCULARITY_ERROR_TITLE),
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
    private TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() {

        @Override
        public void columnAdded(TableColumnModelEvent e) {
        }

        @Override
        public void columnRemoved(TableColumnModelEvent e) {
        }

        @Override
        public void columnMoved(TableColumnModelEvent e) {
        }

        @Override
        public void columnMarginChanged(ChangeEvent e) {
        }

        @Override
        public void columnSelectionChanged(ListSelectionEvent e) {
            selectionListener.valueChanged(e);
        }
    };
    private TableModelListener changeListener = new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent event) {
            getSpreadsheet().getModel().removeTableModelListener(this);
        }
    };
    private ListSelectionListener selectionListener = new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            int row = excelTable.getSelectedRow();
            int column = excelTable.getSelectedColumn();
            Object value = excelTable.getValueAt(row, column);
            if (excelTable instanceof Spreadsheet) {
                value = ((Spreadsheet) excelTable).getExpressionParser().getEditedValue(value);
            }
            textField.setText(value == null ? "" : value.toString());
        }
    };
}
